// FacebookClientSession.cs
// Facebook/FrameworkDesktop/Service
// Copyright (c) 2007, Nikhil Kothari. All Rights Reserved.
//

using System;
using System.Diagnostics;
using System.Windows.Forms;
using Facebook.Service;
using Facebook.Service.Core;

namespace Facebook.Desktop {

    /// <summary>
    /// Represents a Facebook session that is used by a Facebook
    /// client/desktop application.
    /// </summary>
    public sealed class FacebookClientSession : FacebookSession {

        /// <summary>
        /// Creates an instance of a FacebookServerSession with the
        /// specified application information.
        /// </summary>
        /// <param name="appKey">The application key used as an API key.</param>
        /// <param name="secret">The application secret used to sign requests.</param>
        public FacebookClientSession(string appKey, string secret)
            : base(appKey, secret, FacebookApplicationType.Client) {
        }

        /// <summary>
        /// Initializes a new session before any requests can be performed.
        /// This launches a login dialog to allow the user to sign in with Facebook.
        /// The implementation uses the specified authentication token to create a new session key.
        /// </summary>
        /// <param name="parentWindow">The parent window for the login dialog.</param>
        /// <returns>true if the session was initialized; false otherwise.</returns>
        public bool Initialize(IWin32Window parentWindow) {
            FacebookLoginForm loginForm = new FacebookLoginForm(this);

            if (loginForm.ShowDialog(parentWindow) == DialogResult.OK) {
                UpdateSecret(loginForm.Secret);
                InitializeSessionKey(loginForm.SessionKey, loginForm.SessionExpires, FacebookSessionType.User);
                InitializeUserID(loginForm.UserID, null);
                return true;
            }

            return false;
        }
    }
}
